package com.campus.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.web.util.pattern.PathPattern;

/**
 * 雪花id生成工具类
 */
public class SnowFlakeUtil {
    private long machineId;
    private long dataCenterId;

    public SnowFlakeUtil(Long machineId, Long dataCenterId) {
        this.machineId = machineId;
        this.dataCenterId = dataCenterId;
    }

    public SnowFlakeUtil() {
        this(0L,0L);
    }

    private static class IdGenHolder {
        private static final SnowFlakeUtil instance = new SnowFlakeUtil();
    }

    public static SnowFlakeUtil get() {
        return IdGenHolder.instance;
    }

    private Snowflake snowflake = IdUtil.getSnowflake(machineId, dataCenterId);

    public synchronized long id() {
        return snowflake.nextId();
    }

    public static Long getId() {
        return SnowFlakeUtil.get().id();
    }

}
